
package com.yimixian.app.order;

import com.yimixian.app.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class PaymentMethodView extends LinearLayout {

    private List<PaymentMethodItemView> mPaymentMethodItemViewList = new ArrayList<>();

    public PaymentMethodView(Context context) {
        super(context);
        initPayMethodView();
    }

    public PaymentMethodView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPayMethodView();
    }

    public PaymentMethodView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPayMethodView();
    }

    private void initPayMethodView() {
        setOrientation(LinearLayout.VERTICAL);
    }

    public void bind(List<String> paymentMethodNames, String remainingBalance,
            HashMap<String, String> mPaymentMethodTitles, final String mCurrentPayMethodName,
            final OnClickListener onClickListener) {
        mPaymentMethodItemViewList.clear();
        removeAllViews();
        boolean isEnable = paymentMethodNames.size() > 1;
//        for (int i = 0; i < paymentMethodNames.size(); i++) {
//            if (paymentMethodNames.get(i).equals(ConfirmOrderActivity.PAYMENT_METHOD_WECHAT)) {
//                paymentMethodNames.remove(i);
//                paymentMethodNames.add(0, ConfirmOrderActivity.PAYMENT_METHOD_WECHAT);
//            }
//        }

        for (int i = 0; i < paymentMethodNames.size(); i++) {
            String paymentMethodName = paymentMethodNames.get(i);
            final PaymentMethodItemView paymentMethodItemView = new PaymentMethodItemView(
                    getContext());
            paymentMethodItemView.bind(paymentMethodName, remainingBalance, mPaymentMethodTitles);
            if (isEnable) {
                if (paymentMethodName.equals(mCurrentPayMethodName)) {
                    paymentMethodItemView.setChecked(true);
                }
                paymentMethodItemView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        for (PaymentMethodItemView payMethodItemView : mPaymentMethodItemViewList) {
                            payMethodItemView.setChecked(false);
                        }
                        onClickListener.onClick(view);
                        paymentMethodItemView.setChecked(true);
                    }
                });
            } else {
                paymentMethodItemView.setEnabled(false);
                paymentMethodItemView.setCheckImageVisibility(View.GONE);
            }
            mPaymentMethodItemViewList.add(paymentMethodItemView);
            addView(paymentMethodItemView);
            if (i < paymentMethodNames.size() - 1) {
                View divider = new View(getContext());
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT
                        , 1);
                int dividerMargin = getResources().getDimensionPixelOffset(
                        R.dimen.confirm_order_vertical_margin);
                layoutParams.setMargins(dividerMargin,
                        0, 0, 0);
                divider.setBackgroundColor(0xffd3d3d3);
                divider.setLayoutParams(layoutParams);
                addView(divider);
            }
        }
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        for (int i = 0; i < getChildCount(); i++) {
            getChildAt(i).setEnabled(enabled);
        }
    }
}
